package com.sikao.app.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.AddStepPricesSubmitBean;
import com.sikao.app.bean.OwnGoodsDetailBean;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.ui.AddStepPricesActivity;
import com.sikao.app.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品详情进入的阶梯价格
 */
public class MyAddsStepPricesListItemForStateThreeAdapter extends BaseAdapter {

    private List<OwnGoodsDetailBean.ResBean.StepPriceBean> objects = new ArrayList<OwnGoodsDetailBean.ResBean.StepPriceBean>();

    private Context context;
    private LayoutInflater layoutInflater;
    private  String goodsUnit;
    Callback callback;
    public MyAddsStepPricesListItemForStateThreeAdapter(Context context, List<OwnGoodsDetailBean.ResBean.StepPriceBean> objects, Callback callback, String goodsUnit) {
        this.context = context;
        this.objects=objects;
        this.callback=callback;
        this.layoutInflater = LayoutInflater.from(context);
        this.goodsUnit=goodsUnit;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public OwnGoodsDetailBean.ResBean.StepPriceBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private Integer index = -1;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //if (convertView == null) {防止数据复用
            convertView = layoutInflater.inflate(R.layout.my_adds_step_prices_list_item, null);
            convertView.setTag(new ViewHolder(convertView));
        //}
        initializeViews(getItem(position), (ViewHolder) convertView.getTag(),position);

        return convertView;
    }

    private void initializeViews(OwnGoodsDetailBean.ResBean.StepPriceBean object, ViewHolder holder,final  int position) {
        //TODO implement
        holder.deleteBtn.setVisibility(View.GONE);
       /* holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.notice(position);
            }
        });*/

   /*   if("0".equals(AddStepPricesActivity.stepPricesBeanList.get(position).getNumber())){
          holder.etNumber.setHint("请输入数量");
      }else{
          holder.etNumber.setText(AddStepPricesActivity.stepPricesBeanList.get(position).getNumber());
      }
    if("0".equals(AddStepPricesActivity.stepPricesBeanList.get(position).getPrice())){
          holder.etPrice.setHint("请输入价格");
      }else{
          holder.etPrice.setText(AddStepPricesActivity.stepPricesBeanList.get(position).getPrice());
      }*/
  //设置单位显示的值
         holder.tvUnit.setText("("+goodsUnit+"):");
        holder.tvUnitPrice.setText("(元/"+goodsUnit+"):");

        holder.etNumber.setText(objects.get(position).getNumber());
        holder.etPrice.setText(objects.get(position).getPrice());
        holder.etNumber.setEnabled(false);
        holder.etPrice.setEnabled(false);
     /*   holder.etNumber.setOnTouchListener(new View.OnTouchListener() {

            EditText et=null;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                et=(EditText)v;

                et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        String str = et.getText().toString();

                        if (!TextUtils.isEmpty(str)&& Utils.isNum(str)) {
                            AddStepPricesActivity.stepPricesBeanList.get(position).setNumber(str);
                        }
                    }
                });
                return  false;
            }
        });
        holder.etPrice.setOnTouchListener(new View.OnTouchListener() {

            EditText et=null;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                et=(EditText)v;

                et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        String str = et.getText().toString();

                        if (!TextUtils.isEmpty(str)&& Utils.isNum(str)) {
                            AddStepPricesActivity.stepPricesBeanList.get(position).setPrice(str);
                        }
                    }
                });
                return  false;
            }
        });
*/

    }

    protected class ViewHolder {
        private TextView tvNumber;
        private TextView tvUnit;
        private EditText etNumber;
        private TextView tvPrice;
        private TextView tvUnitPrice;
        private EditText etPrice;
        private Button deleteBtn;

        public ViewHolder(View view) {
            tvNumber = (TextView) view.findViewById(R.id.tv_number);
            tvUnit = (TextView) view.findViewById(R.id.tv_unit);
            etNumber = (EditText) view.findViewById(R.id.et_number);
            tvPrice = (TextView) view.findViewById(R.id.tv_price);
            tvUnitPrice = (TextView) view.findViewById(R.id.tv_unit_price);
            etPrice = (EditText) view.findViewById(R.id.et_price);
            deleteBtn = (Button) view.findViewById(R.id.delete_btn);

        }
    }

}
